﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        #region fonctions students
        ////////////////////////FONCTIONS STUDENTS////////////////////////////////


        //Listing des students enregistrés
        private void list_students_offline(ListBox maListe)
        {
            maListe.Items.Clear();

            foreach (Students s in (from e in db_local.Students select e))
            {
                maListe.Items.Add(s.Login);

            }
        }


        //Affichage de la liste des students
        private void display_students_offline(String students)
        {
            foreach (Students s in (from e in db_local.Students select e))
            {
                if (s.Login == students)
                {
                    student_login_textbox.Text = s.Login;
                    student_password_textbox.Password = s.Password;
                    student_firstname_textbox.Text = s.Firstname;
                    student_surname_textbox.Text = s.Surname;

                    student_campus_combobox.Items.Clear();
                    //student_campus_combobox.Items.Add(s.campus);
                    chargement_liste_campus_student_offline();
                    student_campus_combobox.SelectedValue = s.Campus;

                    student_class_combobox.Items.Clear();
                    student_class_combobox.Items.Add(s.Classe);
                    student_class_combobox.SelectedValue = s.Classe;
                }

            }
        }


        //Rafraichissement des éléments
        private void refresh_students_offline()
        {
            student_login_textbox.Text = "";
            student_password_textbox.Password = "";
            student_firstname_textbox.Text = "";
            student_surname_textbox.Text = "";
            list_students(student_listbox);
            chargement_liste_campus_student_offline();
        }


        //Ajout d'un student
        private void add_students_offline()
        {
            Students stud1 = new Students();

            stud1.Login = student_login_textbox.Text.ToLower();
            stud1.Password = student_password_textbox.Password;
            stud1.Firstname = student_firstname_textbox.Text.ToLower();
            stud1.Surname = student_surname_textbox.Text.ToLower();
            stud1.Campus = student_campus_combobox.SelectedItem.ToString().ToLower();
            stud1.Classe = student_class_combobox.SelectedItem.ToString().ToLower();

            stud1.Notepad_enabled = 1;
            stud1.Notepad_text = "Write some notes here";

            db_local.Students.InsertOnSubmit(stud1);
            db_local.SubmitChanges();

            //Rafraichissement des éléments
            refresh_students_offline();


        }


        //Mise à jour d'un student
        private void update_students_offline()
        {
            if (student_login_textbox.Text != "" && student_password_textbox.Password != "" && student_firstname_textbox.Text != "" && student_surname_textbox.Text != "")
            {
                Students stud2 = (from e in db_local.Students
                                  where e.Login == (string)student_listbox.SelectedValue
                                  select e).FirstOrDefault();

                stud2.Login = student_login_textbox.Text.ToLower();
                stud2.Password = student_password_textbox.Password;
                stud2.Firstname = student_firstname_textbox.Text.ToLower();
                stud2.Surname = student_surname_textbox.Text.ToLower();
                stud2.Campus = student_campus_combobox.SelectedItem.ToString().ToLower();
                stud2.Classe = student_class_combobox.SelectedItem.ToString().ToLower();

                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_students_offline();
            }

        }


        //Suppression d'un student
        private void delete_students_offline()
        {
            if (student_login_textbox.Text != "")
            {
                Students stud3 = (from e in db_local.Students
                                  where e.Login == (string)student_listbox.SelectedValue
                                  select e).FirstOrDefault();

                db_local.Students.DeleteOnSubmit(stud3);
                db_local.SubmitChanges();

                //Rafraichissement des éléments
                refresh_students_offline();
            }

        }


        private void sort_students_by_campus_offline()
        {

            string current_selected_campus;

            //On affiche les bons événéments selon le tri effectué
            if (student_sort_combobox.Text != "")
            {
                current_selected_campus = student_sort_combobox.SelectedValue.ToString();

                student_listbox.Items.Clear();

                if (current_selected_campus == "All")
                {
                    foreach (Students student1 in (from e in db_local.Students
                                                  select e))
                    {
                        student_listbox.Items.Add(student1.Login);
                    }

                }

                else
                {
                    foreach (Students student1 in (from e in db_local.Students
                                                  where e.Campus == current_selected_campus
                                                  select e))
                    {
                        student_listbox.Items.Add(student1.Login);
                    }
                }

            }

        }


        private void chargement_liste_campus_student_offline()
        {
            student_campus_combobox.Items.Clear();

            foreach (Campus s in (from e in db_local.Campus select e))
            {
                student_campus_combobox.Items.Add(s.Name);
            }
        }

        private void chargement_liste_classe_student_offline()
        {

            int id_class_temp = (from i in db_local.Campus
                                 where i.Name.Equals(student_campus_combobox.SelectedValue)
                                 select i.Id).FirstOrDefault();

            student_class_combobox.Items.Clear();

            foreach (Classes s in (from e in db_local.Classes where e.Campus_id == id_class_temp select e))
            {
                student_class_combobox.Items.Add(s.Name);
            }

        }


        private void chargement_campus_autorises_student_offline()
        {

            switch (usertype)
            {
                case "administrators":

                    student_sort_combobox.Items.Clear();
                    student_sort_combobox.Items.Add("All");

                    foreach (Campus campus in (from e in db_local.Campus select e))
                    {
                        student_sort_combobox.Items.Add(campus.Name);
                        student_sort_combobox.SelectedItem = student_sort_combobox.Items[0];
                    }

                    break;

                case "managers":

                    student_sort_combobox.Items.Clear();

                    //Récupération, découpage et affichage des campus enregistrés pour un manager
                    string[] table_campus;
                    string registred_campus = usercampus;
                    table_campus = registred_campus.Split('|');

                    for (int i = 0; i < table_campus.Length; i++)
                    {
                        student_sort_combobox.Items.Add(table_campus[i]);
                    }

                    student_sort_combobox.SelectedItem = student_sort_combobox.Items[0];

                    break;

            }
        }

        #endregion
    }
}
